package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kun.index.entity.CollectUserBlog;
import com.kun.index.entity.LikeUserBlog;
import com.kun.index.mapper.LikeUserBlogMapper;
import com.kun.index.service.LikeUserBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 点赞表，用户和博客 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-20
 */
@Service
public class LikeUserBlogServiceImpl extends ServiceImpl<LikeUserBlogMapper, LikeUserBlog> implements LikeUserBlogService {

    @Autowired
    private LikeUserBlogMapper likeUserBlogMapper;

    @Override
    public Integer addLike(LikeUserBlog likeUserBlog) {
        return likeUserBlogMapper.insert(likeUserBlog);
    }

    @Override
    public Integer queryLikeNum(Integer blogId) {
        QueryWrapper<LikeUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",blogId);
        return likeUserBlogMapper.selectList(wrapper).size();
    }

    @Override
    public Integer deleteLike(LikeUserBlog likeUserBlog) {
        QueryWrapper<LikeUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",likeUserBlog.getBlogId());
        wrapper.eq("userId",likeUserBlog.getUserId());
        int num = likeUserBlogMapper.delete(wrapper);
        return num;
    }

    @Override
    public Integer checkLikeStatus(LikeUserBlog likeUserBlog) {
        QueryWrapper<LikeUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",likeUserBlog.getBlogId());
        wrapper.eq("userId",likeUserBlog.getUserId());
        int num = likeUserBlogMapper.selectCount(wrapper);
        return num;
    }

    @Override
    public List<LikeUserBlog> queryLikeUserBlogByConditions(HashMap<String, Object> map) {
        QueryWrapper<LikeUserBlog> wrapper = new QueryWrapper<>();
        if (map.get("userId")!=null){
            Integer userId = (Integer) map.get("userId");
            wrapper.eq("userId",userId);
        }
        if (map.get("blogId")!=null){
            Integer blogId = (Integer) map.get("blogId");
            wrapper.eq("blogId",blogId);
        }

        return likeUserBlogMapper.selectList(wrapper);
    }
}
